import instance from './axios.js'

const post = (url, data, contentType) => {
    return new Promise((resolve, reject) => {
        instance.defaults.headers['Content-Type'] = contentType ? contentType : 'application/json'
        instance.post(url, data)
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}

const get = (url, data) => {
    return new Promise((resolve, reject) => {
        instance.get(url, {params: data})
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}

const put = (url, data, contentType) => {
    return new Promise((resolve, reject) => {
        instance.defaults.headers['Content-Type'] = contentType ? contentType : 'application/json'
        instance.put(url, data)
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}

const patch = (url, data, contentType) => {
    return new Promise((resolve, reject) => {
        instance.defaults.headers['Content-Type'] = contentType ? contentType : 'application/json'
        instance.patch(url, patch)
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}

const del = (url, data) => {
    return new Promise((resolve, reject) => {
        instance.delete(url, {params: data})
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}

export default {
    post, get, put, patch, del
}